package com.peanut.baby.mvp.dingyue;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.MyTag;
import com.peanut.baby.model.User;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

public class DingyuePresenter implements DingyueContract.Presenter{
    private Activity activityl;
    private DingyueContract.View view;
    public DingyuePresenter(Activity activityl,DingyueContract.View view) {
        this.activityl = activityl;
        this.view = view;
    }

    @Override
    public void getHotTags() {
        Observable obs = RetrofitClient.getInstance().getHotTags(InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<List<String>>compose())
                .compose(RxLifecycle.bind(activityl).withObservable())
                .subscribe(new BaseObserver<List<String>>() {
                    @Override
                    protected void onHandleSuccess(List<String> o) {
                        view.onHotTagsGet(true,o,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onHotTagsGet(false,null,msg);
                    }
                });
    }

    @Override
    public void getRecommendTags() {
        Observable obs = RetrofitClient.getInstance().getRecommendTags(InitManager.getInstance().getUserId(),InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<MyTag>compose())
                .compose(RxLifecycle.bind(activityl).withObservable())
                .subscribe(new BaseObserver<MyTag>() {
                    @Override
                    protected void onHandleSuccess(MyTag o) {
                        view.onRecommendTagsGet(true,o,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onRecommendTagsGet(false,null,msg);
                    }
                });
    }

    @Override
    public void subscribeTags(String tags) {
        Observable obs = RetrofitClient.getInstance().setTabsSubscribe(InitManager.getInstance().getUserId(),tags,InitManager.getInstance().getUserToken());
        obs.compose(BaseScheduler.<MyTag>compose())
                .compose(RxLifecycle.bind(activityl).withObservable())
                .subscribe(new BaseObserver<MyTag>() {
                    @Override
                    protected void onHandleSuccess(MyTag o) {
                        view.onSubscribeResult(true);
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onSubscribeResult(false);
                    }
                });
    }
}
